﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FineUI;
using OnlineTestBLL;
using OnlineTestModel;

namespace OnlineTestUI.BackManager
{
    public partial class updatechoice : System.Web.UI.Page
    {
        QuestionsBLL qb = new QuestionsBLL();
        QuestionItemsBLL qib = new QuestionItemsBLL();
        private StudentQuestionsBLL sqb = new StudentQuestionsBLL();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (string.IsNullOrEmpty(Request.QueryString["id"]))
                {
                    int id = Convert.ToInt32(Request.QueryString["id"]);
                    Questions q = qb.GetModel(id);
                    tbblankline.Text = q.BlankLine.ToString();
                    tbchapter.Text = q.Chapter;
                    tbpart.Text = q.Part;
                    tbpartitiondegree.Text = q.PartitionDegree;
                    tbpredifficult.Text = q.PreDifficult;
                    tbquestion.Text = q.QuestionName;
                    taanswer.Text = q.CorrectAnswer;
                    hfparentid.Text = q.ParentId.ToString();
                    hfversionnumber.Text = q.VersionNumber.ToString();
                }
                else
                {
                    Alert.Show("<script>alert('出现错误！');history.back(1); </script>");
                }
            }
           
        }


        protected void BtUpdate_Click(object sender, EventArgs e)
        {
            //如果没有人考过该题的话，可以修改，否则每次对试题的修改都是创建新题
            int id = Convert.ToInt32(hfId.Text);
            int rowCount = sqb.GetRecordCount(" questionid ="+id.ToString());
            if (rowCount>0)//添加新题
            {
                #region // 添加新题
                Questions q = new Questions();
                q.QuestionName = tbquestion.Text;
                q.VersionNumber = Convert.ToInt32(hfversionnumber.Text)+1;
                q.PreDifficult = tbpredifficult.Text;
                q.Chapter = tbchapter.Text;
                q.Part = tbpart.Text;
                q.QuestionTypeId = 1;
                q.BlankLine = Convert.ToInt32(tbblankline.Text);
                q.CorrectAnswer = taanswer.Text;
                q.PartitionDegree = tbpartitiondegree.Text;
                if (hfparentid.Text == "0")//如果没有父id的话，说明其是第一次被修改
                {
                    q.ParentId = id;
                }
                else//否则就是多次被修改，所有的父id应该相同
                {
                    q.ParentId = Convert.ToInt32(hfparentid.Text);
                }
                q.Status = 1;
                //重新插入所有选项
                string[] s = Request["hidProperty"].Split(new char[] { '&' }, System.StringSplitOptions.RemoveEmptyEntries);
                try
                {
                    using (TransactionScope ts = new TransactionScope())
                    {
                        qb.UpdateStatus(0, q.ParentId);//修改旧题的status为0，作废，
                        int result = qb.Add(q);
                        if (result > 0)
                        {
                            List<QuestionItems> questionItemList = new List<QuestionItems>();
                            for (int i = 0; i < s.Length; i++)
                            {
                                string[] ss = s[i].Split(new char[] { '|' });
                                QuestionItems qi = new QuestionItems();
                                qi.ItemGrade = Convert.ToInt32(ss[1]);
                                qi.ItemName = ss[0];
                                qi.QuestionId = result;
                                qib.Add(qi);
                            }
                          
                            ts.Complete();
                            Alert.Show("添加填空题成功");
                        }
                        else
                        {
                            Alert.Show("出现错误");
                        }

                    }

                }
                catch (Exception)
                {
                    Alert.Show("出现错误");
                }
                #endregion
               
            }
            else//对旧题进行修改
            {
                #region
                Questions q = new Questions();
                q.Id = Convert.ToInt32(hfId.Text);
                q.QuestionName = tbquestion.Text;
                q.VersionNumber = Convert.ToInt32(hfversionnumber.Text);
                q.PreDifficult = tbpredifficult.Text;
                q.Chapter = tbchapter.Text;
                q.Part = tbpart.Text;
                q.QuestionTypeId = 1;
                q.BlankLine = Convert.ToInt32(tbblankline.Text);
                q.CorrectAnswer = taanswer.Text;
                q.PartitionDegree = tbpartitiondegree.Text;
                q.ParentId = Convert.ToInt32(hfparentid.Text);
                q.Status = 1;
                //重新插入所有选项
                string[] s = Request["hidProperty"].Split(new char[] { '&' }, System.StringSplitOptions.RemoveEmptyEntries);
                try
                {
                    using (TransactionScope ts = new TransactionScope())
                    {
                        bool result = qb.Update(q);
                        if (result)
                        {
                            for (int i = 0; i < s.Length; i++)//这里还有问题，应该是新选项加进去，旧选项保留
                            {
                                string[] ss = s[i].Split(new char[] { '|' });
                                QuestionItems qi = new QuestionItems();
                                qi.ItemGrade = Convert.ToInt32(ss[1]);
                                qi.ItemName = ss[0];
                                qi.QuestionId = q.Id;
                                qib.Add(qi);
                            }
                          
                            ts.Complete();
                            Alert.Show("添加填空题成功");
                        }
                        else
                        {
                            Alert.Show("出现错误");
                        }

                    }

                }
                catch (Exception)
                {
                    Alert.Show("出现错误");
                }
                #endregion
            }
            

        }
    }
}